package com.china.produce.controller;

import com.china.produce.pojo.ThreadPoolConfigEntity;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.redisson.api.listener.MessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName
 * @Author niujie
 * @Version
 * @Description
 * @CreateTime 2022/11/13
 */
@RequestMapping("/redisson")
@RestController
@Slf4j
public class RedissonController {

    @Autowired
    @Qualifier("redissonClient")
    private RedissonClient redisson;

    @PostMapping("/testLock")
    public String testLock() {
        log.info("开始进入----");
        RLock lock = redisson.getLock("test-lock");
        lock.lock();
        try {
            Thread.sleep(5000);
            log.info("加锁成功，执行后续代码。线程 ID: {}", Thread.currentThread().getId());
        }catch (Exception exception) {
            log.error("{}", exception.getMessage());
        }finally {
            lock.unlock();

           log.info("Finally，释放锁成功。线程 ID：{}", Thread.currentThread().getId());
        }

        return "test lock ok";
    }

    @PostMapping("/lockRedisson")
    public String lockRedisson() {
        log.info("lock redisson start -----");

        RLock lock = redisson.getLock("test-");
        lock.lock();
        try {
            TimeUnit.SECONDS.sleep(5);

            log.info("deal with doing ---");
        }catch (Exception exception) {

            log.error("deal with error");
        }finally {
            lock.unlock();
        }

        return "success to deal with lock";
    }
}
